package com.semidata.trp.material.http;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;

import com.semidata.rtd.core.http.server.AbstractDispatchHandler;
import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.material.config.Config;
import com.semidata.trp.material.service.MaterialService;

public class MaterialReviewHttpHandler extends AbstractDispatchHandler{

    private String type;
    private String name;

    public MaterialReviewHttpHandler(ChannelHandlerContext ctx, HttpRequest request, String name, String type) {
        super(ctx, request);
        this.name = name;
        this.type = type;
    }

    @Override
    public void handle(HttpSession session) {
        Logger.log("DEBUG", "review start");
        byte[] ret = (byte[]) MaterialService.review(name + '.' + type);
        if (ret.length == 0) {
            session.getResponse().error();
        }
        session.getResponse().setHeader(HttpHeaders.Names.CONTENT_TYPE, Config.getConfig().getAvailableImageTypes().get(type));
        session.getResponse().end(Unpooled.wrappedBuffer(ret));
    }

}
